package hxy.bytecode.algorithm.tree;

import lombok.Data;

/**
 * @since 2.1
 * 两个二叉树合并
 * https://mp.weixin.qq.com/s/Xu-VdXopd536pCMaQ4rJ1g
 * @author eric
 */
public class MergeTree {
    public static void main(String[] args) {

        TreeNode root = new TreeNode(1);
        TreeNode left1 = new TreeNode(1);
        TreeNode right2 = new TreeNode(1);

        root.setLeft(left1);
        root.setRight(right2);


        TreeNode left3 = new TreeNode(1);
        TreeNode right4 = new TreeNode(1);

        left1.setLeft(left3);
        left1.setRight(right4);


        TreeNode left5 = new TreeNode(1);
        TreeNode right6 = new TreeNode(1);

        right2.setLeft(left5);
        right2.setRight(right6);

        TreeNode left7 = new TreeNode(1);
        TreeNode right8 = new TreeNode(1);
        left3.setLeft(left7);
        left3.setRight(right8);

        TreeNode right5 = new TreeNode(1);
        right4.setRight(right5);


        TreeNode treeNode = mergeTrees(root, root);
        System.out.println(treeNode.val);


    }


    public static TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
        if (t1 == null)
            return t2;
        if (t2 == null)
            return t1;
        t1.val += t2.val;
        t1.left = mergeTrees(t1.left, t2.left);
        t1.right = mergeTrees(t1.right, t2.right);
        return t1;
    }

}

/**
 * 二叉树的类
 */
@Data
class TreeNode {
    public TreeNode left;
    public TreeNode right;
    public int val;

    public TreeNode(){

    }

    public TreeNode(int val){
    this.val=val;
    }

    public TreeNode(TreeNode left,TreeNode right,int val){
        this.left=left;
        this.right=right;
        this.val=val;
    }
}